package com.myh.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.myh.gulimall.product.entity.AttrEntity;
import com.myh.gulimall.product.service.AttrGroupService;
import com.myh.gulimall.product.vo.AttrGroupRelationVo;
import com.myh.gulimall.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myh.common.utils.PageUtils;
import com.myh.common.utils.Query;

import com.myh.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.myh.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.myh.gulimall.product.service.AttrAttrgroupRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveAttrAttrgroupRelation(Long id, AttrVo attr) {
        AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
        entity.setAttrGroupId(attr.getAttrGroupId());
        entity.setAttrId(id);
        attrAttrgroupRelationDao.insert(entity);
    }

    @Override
    public Long getAttrGroupIdByAttrId(Long attrId) {
        QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attr_id", attrId);
        AttrAttrgroupRelationEntity entity = attrAttrgroupRelationDao.selectOne(queryWrapper);
        if (entity != null){
            return entity.getAttrGroupId();
        } else {
            return null;
        }

    }

    @Override
    public void updateAttrGroupIdByAttrId(Long attrId, Long attrGroupId) {
        attrAttrgroupRelationDao.updateAttrGroupIdByAttrId(attrId, attrGroupId);
    }

    @Override
    public List<Long> getAttrIdsByAttrgroupId(Long attrgroupId) {
//        //方式一：
//        QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("attr_group_id", attrgroupId);
//        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationDao.selectList(queryWrapper);
//        List<Long> attrIds = attrAttrgroupRelationEntities.stream().map((entity) -> {
//            Long attrId = entity.getAttrId();
//            return attrId;
//        }).collect(Collectors.toList());

        //方式二：
        List<Long> attrIds = attrAttrgroupRelationDao.getAttrIdsByAttrgroupId(attrgroupId);
        return attrIds;
    }

    @Override
    public Integer selectCountByAttrId(Long attrId) {
        Integer count = attrAttrgroupRelationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
        return count;
    }

    @Override
    public void deleteByIds(List<AttrGroupRelationVo> vos) {
        attrAttrgroupRelationDao.deleteBatchRelation(vos);
    }

    @Override
    public List<Long> getAttrIds(List<AttrEntity> attrEntities) {
        List<Long> list = attrEntities.stream().filter((attrEntity) -> {
            Long attrGroupIdByAttrId = getAttrGroupIdByAttrId(attrEntity.getAttrId());
            return attrGroupIdByAttrId != null;
        }).map((attrEntity) -> {
            return attrEntity.getAttrId();
        }).collect(Collectors.toList());
        return list;
    }

}